package com.marry.JMail;

import com.marry.JMail.Entity.MailEntity;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.util.List;
import java.util.Properties;

/**
 * @author ml
 * @create 2017-06-20--16:49
 */
public class MailSender {

    //邮件实体
    private static MailEntity mail = new MailEntity();

    /**
     * 设置邮件标题
     *
     * @param title
     * @return
     */
    public MailSender title(String title) {
        mail.setTitle(title);
        return this;
    }


    public MailSender content(String content) {
        mail.setContent(content);
        return this;

    }

    public MailSender contentType(String contentType) {
        mail.setContentType(contentType);
        return this;
    }


    public MailSender targets(List<String> targets) {
        mail.setList(targets);
        return this;
    }


    public void send() throws Exception {

        if (mail.getContentType() == null) {
            mail.setContentType(MailContentTypeEnum.HTML.getValue());

        }

        if (mail.getTitle() == null || mail.getTitle().trim().length() == 0) {
            throw new Exception("邮件标题没有设置");
        }

        if (mail.getContent() == null || mail.getContent().trim().length() == 0) {
            throw new Exception("邮件内容没有设置");
        }

        if (mail.getList().size() == 0) {
            throw new Exception("没有填写接受者地址");

        }

        //读取配置文件
        final PropertiesUtil properties = new PropertiesUtil("mail");

        //创建Properties类用以记录邮箱的一些属性
        final Properties props = new Properties();

        //发送SMTP邮件，必须进行身份验证
        props.setProperty("mail.smtp.auth", "true");

        //服务器地址
        props.setProperty("mail.smtp.host", properties.getValue("mail.smtp.service"));

        //设置端口号
        props.setProperty("mail.smtp.port", properties.getValue("mail.smtp.port"));

        //设置发送邮箱
        props.setProperty("mail.user", properties.getValue("mail.from.address"));

        //设置发送邮箱的16位smtp口令
        props.setProperty("mail.password", properties.getValue("mail.from.smtp.pwd"));

        //构建授权信息，进行SMTP身份验证
        Authenticator authenticator = new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {

                String userName = props.getProperty("mail.user");
                String password = props.getProperty("mail.password");
                return new PasswordAuthentication(userName, password);
            }
        };
        //使用环境属性和授权信息，创建邮件对话
        Session mailSession = Session.getInstance(props, authenticator);

        //c创建邮件消息
        MimeMessage message = new MimeMessage(mailSession);

        //设置发件人
        String nickName = MimeUtility.encodeText(properties.getValue("mail.from.nickname"));

        InternetAddress form = new InternetAddress(nickName + "<" + props.getProperty("mail.user") + ">");
        message.setFrom(form);

        //设置邮件标题
        message.setSubject(mail.getTitle());

        //html发送邮件
        if (mail.getContentType().equals(MailContentTypeEnum.HTML.getValue())) {
            //设置邮件内容
            message.setContent(mail.getContent(), mail.getContentType());
        }
        //文本发送文件
        else if (mail.getContentType().equals(MailContentTypeEnum.TEXT.getValue())) {
            message.setText(mail.getContent());
        }

        //发送邮件地址
        List<String> targets = mail.getList();
        for (int i = 0; i < targets.size(); i++) {

            try {
                //设置收件人邮件
                InternetAddress to = new InternetAddress(targets.get(i));
                message.setRecipient(Message.RecipientType.TO, to);
                Transport.send(message);
            } catch (MessagingException e) {
                e.printStackTrace();
            }
        }


    }


}
